# -*- coding: utf-8 -*-
"""
Created on Thu Sep 19 10:57:07 2019

@author: Administrator
"""

# 1028 人口普查
# 思路：循环，筛选合理的生日，判断最大最小值
# 注意：
# 1.time模块中转换时间戳，时间需要在1970-01-01以后，如果在这之前，转换失败
# 2.求最大最小生日时，时间戳最大的是最年轻的生日
# 3.坑：如果没有有效生日，输出为0
# 4.运行超时：优化转换时间戳直接直接编码

# int()类型转换
# int('')：ValueError: invalid literal for int() with base 10: ''
# float(''):ValueError: could not convert string to float: 
# complex(''):ValueError: complex() arg is a malformed string
# str.split():默认为空格分隔符
# 运行超时
"""
def test():
    raw_input = input()
    if raw_input == '':
        return
    num = int(raw_input)
    # 输入格式已经告诉num的范围为(0,10^5]，所以不用验证输入了
#    if num <= 0 or num > 10 ** 5:
#        return
    raw = []
    separator = '/'
    today = '2014/09/06'
    base = '1814/09/06'
    today_timestamp = timestamp(today, separator)
    base_timestamp = timestamp(base, separator)
    # 时间转为时间戳
    for i in range(num):
        # 不用验证输入
        temp = input().split(' ')
        temp_timestamp = timestamp(temp[1], separator)
        # 过滤不合理生日
        if temp_timestamp >= base_timestamp and temp_timestamp <= today_timestamp:
            temp[1] = temp_timestamp
            raw.append(temp)
    
    vaild_date = len(raw)
    if vaild_date != 0:
        max_birth, min_birth = max_min_birth(raw)
        print("%d %s %s" % (vaild_date, max_birth[0], min_birth[0]))
    else:
        # 坑：如果没有有效生日，输出0
        print("%d" % vaild_date)


# 根据时间转换成时间戳
def timestamp(time_str, separator):
    timestamp = 0
    digits = time_str.split(separator)
    for digit in digits:
        timestamp = timestamp * 60 + int(digit)
    return timestamp

#print(timestamp('3:15:6', ':'))

def max_min_birth(raw):
    # 求最大、最小生日
    max_timestamp = 0
    max_birth = []
    min_timestamp = float('inf')
    min_birth = []
    for birth in raw:
        if birth[1] > max_timestamp:
            max_timestamp = birth[1]
            min_birth = birth
        if birth[1] < min_timestamp:
            min_timestamp = birth[1]
            max_birth = birth
    return max_birth, min_birth

#raw = []
#raw = [['John', 12323232], ['Tome', 12323231],['Tony', 12423232]]
#print(max_min_birth(raw))
test()
"""
# 运行超时：优化转换时间戳直接编码
"""
def test_adv():
    raw_input = input()
    if raw_input == '':
        return
    num = int(raw_input)

    raw = []
    today = '2014/09/06'
    base = '1814/09/06'

    for i in range(num):
        temp = input().split(' ')
        # 过滤不合理生日
        if temp[1] >= base and temp[1] <= today:
            raw.append(temp)
    
    vaild_date = len(raw)
    if vaild_date != 0:
        # 求最大、最小生日
        max_timestamp = '0'
        max_birth = []
        min_timestamp = 'inf'
        min_birth = []
        for birth in raw:
            if birth[1] > max_timestamp:
                max_timestamp = birth[1]
                min_birth = birth
            if birth[1] < min_timestamp:
                min_timestamp = birth[1]
                max_birth = birth
        
        print("%d %s %s" % (vaild_date, max_birth[0], min_birth[0]))
    else:
        # 坑：如果没有有效生日，输出0
        print("%d" % vaild_date)
        
test_adv()
"""